/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package usuario.control;

import bibliinfo.FrameMain;
import java.util.Calendar;
import javax.swing.JOptionPane;
import usuario.model.Contato;
import usuario.model.Data;
import usuario.model.Endereco;
import usuario.model.UsuarioDao;
import usuario.model.ValidaUsuario;

/**
 *
 * @author Ar0n
 */
public class UserFrameDadosSave extends javax.swing.JInternalFrame {

    /**
     * Creates new form UserFrameDadosSalve
     */
    public UserFrameDadosSave() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jBLimpar = new javax.swing.JButton();
        jBCancel = new javax.swing.JButton();
        jBSave = new javax.swing.JButton();

        jBLimpar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jBLimpar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/img_user/1365287991_edit-clear.png"))); // NOI18N
        jBLimpar.setText("Limpar Campos");
        jBLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBLimparActionPerformed(evt);
            }
        });

        jBCancel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jBCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/img_user/1365683699_window-close.png"))); // NOI18N
        jBCancel.setText("Cancelar");
        jBCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCancelActionPerformed(evt);
            }
        });

        jBSave.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jBSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/img_user/1365288060_3floppy_umount.png"))); // NOI18N
        jBSave.setText("Salvar");
        jBSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jBLimpar, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 185, Short.MAX_VALUE)
                .addComponent(jBCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jBSave, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBLimpar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBSave, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(66, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancelActionPerformed
       jBLimparActionPerformed(evt);
        
       FrameMain.userFrameDados.setVisible(false);
       FrameMain.userFrameDados.setEstado(false);
       
       FrameMain.userFrameMain.setVisible(true);
       FrameMain.userFrameMain.setEstado(true); 
        
       FrameMain.formata(11);
        
    }//GEN-LAST:event_jBCancelActionPerformed

    private void jBLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBLimparActionPerformed
        limparCampos();
    }//GEN-LAST:event_jBLimparActionPerformed

    private void jBSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSaveActionPerformed
               
        ValidaUsuario validarUsuario = new ValidaUsuario();
        boolean resp = validarUsuario.validaUser(FrameMain.userFrameDados.jTRG, FrameMain.userFrameDados.jTNome,
                FrameMain.userFrameDados.jTCidade, FrameMain.userFrameDados.jTCep, FrameMain.userFrameDados.jTBairro,
                FrameMain.userFrameDados.jTRua, FrameMain.userFrameDados.jTNumCasa, FrameMain.userFrameDados.jTDia,
                FrameMain.userFrameDados.jTMes, FrameMain.userFrameDados.jTAno);
        
        if(resp == true){
            usuario.model.Usuario usuario = new usuario.model.Usuario();        
            usuario.setRg(Long.parseLong(FrameMain.userFrameDados.jTRG.getText()));
            usuario.setNome(FrameMain.userFrameDados.jTNome.getText());                
            usuario.setSexo(FrameMain.userFrameDados.jCBSexo.getSelectedItem().toString());        
            usuario.setObservacoes(FrameMain.userFrameDados.jTAObs.getText());
            usuario.setAtivo(true);
            usuario.setSuspenso(false);

            Endereco endereco = new Endereco();
            endereco.setRg(Long.parseLong(FrameMain.userFrameDados.jTRG.getText()));
            endereco.setEstado(FrameMain.userFrameDados.jCBEstado.getSelectedItem().toString());        
            endereco.setCidade(FrameMain.userFrameDados.jTCidade.getText());            
            endereco.setCep(FrameMain.userFrameDados.jTCep.getText());
            endereco.setBairro(FrameMain.userFrameDados.jTBairro.getText());
            endereco.setRua(FrameMain.userFrameDados.jTRua.getText());
            endereco.setNumCasa(FrameMain.userFrameDados.jTNumCasa.getText());

            Contato contato = new Contato();
            contato.setRg(Long.parseLong(FrameMain.userFrameDados.jTRG.getText()));
            contato.setTel_1(FrameMain.userFrameDados.jTTel_1.getText());
            contato.setTel_2(FrameMain.userFrameDados.jTTel_2.getText());
            contato.setTel_3(FrameMain.userFrameDados.jTTel_3.getText());
            contato.setTel_4(FrameMain.userFrameDados.jTTel_4.getText());
            contato.setEmail_1(FrameMain.userFrameDados.jTEmail_1.getText());
            contato.setEmail_2(FrameMain.userFrameDados.jTEmail_1.getText());

            Data data = new Data();
            data.setRg(Long.parseLong(FrameMain.userFrameDados.jTRG.getText()));

            //PEGANDO DATA PREENCHIDA
            int year = Integer.parseInt(FrameMain.userFrameDados.jTAno.getText());
            int month = Integer.parseInt(FrameMain.userFrameDados.jTMes.getText());
            int date = Integer.parseInt(FrameMain.userFrameDados.jTDia.getText());

            data.calendar.set(year, month - 1, date);//a contagem do mes começa de zero
            data.instanciarDataNasc();
            data.InstanciarDataNascSQL();               

            //PEGANDO DATA DO SISTEMA OPERACIONAL
            data.calendar.set(Calendar.getInstance().get(Calendar.YEAR), 
                    Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH));

            data.instanciarDataCad();
            data.instanciarDataCadSQL();       

            data.calendar.set(0002, 00, 00);
            data.instanciarDataInicioMulta();
            data.instanciarDataInicioMultaSQL();
            
            data.instanciarDataFimMulta();
            data.instanciarDataFimMultaSQL();            
            
            //gravando no banco de dados
            UsuarioDao userDao = new UsuarioDao();
            userDao.create(usuario, endereco, contato, data);

            //fechando frame e limpando campos
            JOptionPane.showMessageDialog(FrameMain.userFrameDados, "O cadastro foi realizado com sucesso", "", 1);

            jBLimparActionPerformed(evt);
            jBCancelActionPerformed(evt);

            FrameMain.userFrameDados.setVisible(false);
            FrameMain.userFrameDados.setEstado(false);
        }        
    }//GEN-LAST:event_jBSaveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton jBCancel;
    public javax.swing.JButton jBLimpar;
    public javax.swing.JButton jBSave;
    // End of variables declaration//GEN-END:variables
    
    //métodos
    public void limparCampos(){
    FrameMain.userFrameDados.jTRG.setText("");
        FrameMain.userFrameDados.jTNome.setText("");
        FrameMain.userFrameDados.jTDia.setText("");
        FrameMain.userFrameDados.jTMes.setText("");
        FrameMain.userFrameDados.jTAno.setText("");
        FrameMain.userFrameDados.jCBSexo.setSelectedIndex(0);
        
        FrameMain.userFrameDados.jCBEstado.setSelectedIndex(0);
        FrameMain.userFrameDados.jTCidade.setText("");
        FrameMain.userFrameDados.jTCep.setText("");
        FrameMain.userFrameDados.jTBairro.setText("");
        FrameMain.userFrameDados.jTRua.setText("");        
        FrameMain.userFrameDados.jTNumCasa.setText("");
        
        FrameMain.userFrameDados.jTTel_1.setText("");
        FrameMain.userFrameDados.jTTel_2.setText("");
        FrameMain.userFrameDados.jTTel_3.setText("");
        FrameMain.userFrameDados.jTTel_4.setText("");
        FrameMain.userFrameDados.jTEmail_1.setText("");
        FrameMain.userFrameDados.jTEmail_2.setText("");
        
        FrameMain.userFrameDados.jTAObs.setText("");
    }
}
